package com.cdxy.service.impl;

import com.cdxy.dmr.dataObject.AxUserDO;
import com.cdxy.dmr.dataObject.AxUserProfileDO;
import com.cdxy.dmr.repository.AxUserProfileRepository;
import com.cdxy.dmr.repository.AxUserRepository;
import com.cdxy.enums.*;
import com.cdxy.enums.redis.RedisCacheGroupEnum;
import com.cdxy.model.dto.base.exception.BusinessException;
import com.cdxy.model.dto.base.redis.RedisCacheManager;
import com.cdxy.model.dto.req.LoginAccountReq;
import com.cdxy.model.dto.req.LoginEmailCodeReq;
import com.cdxy.model.dto.req.LoginMobileCodeReq;
import com.cdxy.model.dto.req.UserWithLoginReq;
import com.cdxy.model.dto.resp.UserInfoResp;
import com.cdxy.service.*;
import com.cdxy.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @Author: tangy
 * @Date: 2020/3/6 16:20
 */
@Service
public class LoginServiceImpl implements ILoginService {

    @Autowired
    private ISmsService smsService;

    @Autowired
    private IEmailService emailService;

    @Autowired
    private IImgService imgService;

    @Autowired
    private AxUserProfileRepository axUserProfileRepository;

    @Autowired
    private AxUserRepository axUserRepository;

    @Autowired
    private RedisCacheManager redisCacheManager;

    @Override
    public UserInfoResp mobileCodeLogin(LoginMobileCodeReq loginMobileCodeReq) {
        String phone = loginMobileCodeReq.getPhone();
        String code = loginMobileCodeReq.getPhoneCode();

        AxUserDO axUserDO = axUserRepository.getByMobile(phone);
        if (axUserDO == null) {
            throw new BusinessException(BizErrorEnum.LOGIN_ACCOUNT_NOT_EXIST);
        }
        // 登录
        Integer axUid = axUserDO.getAxUid();

        smsService.verifySms(KaptchaEnum.BusinessCode.LOGIN_SMS, phone, code);

        return doAfterSuccessLogin(axUid);
    }

    @Override
    public UserInfoResp emailCodeLogin(LoginEmailCodeReq loginEmailCodeReq) {
        String email = loginEmailCodeReq.getEmail();
        String code = loginEmailCodeReq.getEmailCode();

        AxUserDO axUserDO = axUserRepository.getByEmail(email);
        if (axUserDO == null) {
            throw new BusinessException(BizErrorEnum.LOGIN_BIND_EMAIL);
        }

        emailService.verifyEmail(KaptchaEnum.BusinessCode.LOGIN_EMAIL, email, code);


        // 登录
        Integer axUid = axUserDO.getAxUid();
        return doAfterSuccessLogin(axUid);
    }

    @Override
    public UserInfoResp accountLogin(LoginAccountReq loginAccountReq) {
        String account = loginAccountReq.getAccount();
        String password = loginAccountReq.getPassword();
        String uuid = loginAccountReq.getUuid();
        String code = loginAccountReq.getUuidCode();

        AxUserDO axUserDO;
        if (CommonUtil.checkPhone(account)) {
            axUserDO = axUserRepository.getByMobile(account);
        } else {
            axUserDO = axUserRepository.getByEmail(account);
        }
        if (axUserDO == null) {
            throw new BusinessException(BizErrorEnum.LOGIN_ACCOUNT_NOT_EXIST);
        }

        if (StringUtils.isNotBlank(uuid)) {
            imgService.checkImgCode(RedisCacheGroupEnum.LOGIN_IMG_CODE, uuid, code);
        }

        if (!axUserDO.getPassword().equals(password)) {
            throw new BusinessException(BizErrorEnum.LOGIN_PASSWORD_WRONG);
        }

        return doAfterSuccessLogin(axUserDO.getAxUid());
    }


    private UserInfoResp doAfterSuccessLogin(Integer axUid) {
        AxUserDO axUserDO = axUserRepository.getById(axUid);
        String mobile = axUserDO.getMobile();
        String role = axUserDO.getRole();

        AxUserProfileDO axUserProfileDO = axUserProfileRepository.getById(axUid);
        UserInfoResp userInfoResp = BeanUtil.copyPropertiesByFastJson(axUserProfileDO, UserInfoResp.class);
        userInfoResp.setMobile(mobile);
        userInfoResp.setRole(role);

        return userInfoResp;
    }

    @Override
    public void loginOut(UserWithLoginReq userWithLoginReq) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String authorization = attributes.getRequest().getHeader("Authorization");
        redisCacheManager.delete(RedisCacheGroupEnum.LOGIN_SUCCESS_TOKEN, authorization);
    }

}
